package com.ftf.tp.message.interfaces.consumer;

import com.alibaba.fastjson.JSONObject;
import com.ftf.tp.common.constants.NoticeTypeEnum;
import com.ftf.tp.common.constants.RocketMqConstant;
import com.ftf.tp.message.application.dto.message.SupportMessage;
import com.ftf.tp.message.application.service.NoticeApplicationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author tanghao
 * @date 2022/9/1
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = RocketMqConstant.SUPPORT_TOPIC, consumerGroup = RocketMqConstant.TP_MESSAGE_SUPPORT_CONSUMER_GROUP)
public class SupportActionConsumer implements RocketMQListener<SupportMessage>, RocketMQPushConsumerLifecycleListener {

    @Resource
    private NoticeApplicationService noticeApplicationService;

    @Override
    public void onMessage(SupportMessage message) {
        //取消点赞不生成通知
        if (!message.getData().getAction().equals(NoticeTypeEnum.SUPPORT.getCode())) {
            log.info("取消点赞 ,{}", JSONObject.toJSONString(message.getData()));
            return;
        }
        noticeApplicationService.noticeEventCreate(message);
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        consumer.setMaxReconsumeTimes(3);
    }
}
